#include "common.h"

vector<int> findAnagrams(string s, string p)
{
	vector<int> ans;
	int n = p.size();
	vector<int> nums_of_p(128, 0);
	vector<int> flg(128, 0);
	int m = 0;
	for (int i = 0; i < n; i++)
	{
		nums_of_p[p[i]]++;
	}
	int left = 0, right = 0;
	while (right < s.size())
	{
		if (nums_of_p[s[right]] == 0)
		{
			while (left < right)
			{
				flg[s[left]]--;
				m--;
				left++;
			}
			right++;
			left = right;
		}
		else
		{
			flg[s[right]]++;
			m++;
			while (flg[s[right]] > nums_of_p[s[right]])
			{
				flg[s[left]]--;
				m--;
				left++;
			}
			right++;
			if (m == n)
				ans.push_back(left);
		}
	}
	return ans;
}
